<template>
  <div class="content">
    <ul class="category">
      <li
        v-for="item of category"
        :key="item.tab"
        :class="{
          category__item: true,
          category__item_active: item.tab === currentTab
        }"
        @click="handleTabClick(item.tab)"
      >
        {{ item.name }}
      </li>
    </ul>
    <div class="product">
      <div class="product__item" v-for="item of list" :key="item._id">
        <img :src="item.imgUrl" alt="" class="product__item__img" />
        <div class="product__item__detail">
          <h4 class="product__item__title">{{ item.name }}</h4>
          <p class="product__item__sales">月售:{{ item.sales }}件</p>
          <p class="product__item__price">
            <span class="product__item__yen">&yen;{{ item.price }}</span>
            <span class="product__item__origin">&yen;{{ item.oldPrice }}</span>
          </p>
        </div>
        <div class="product__number">
          <span
            class="product__number__minus"
            @click="
              () => {
                ChangeCartItem(shopId, item._id, item, -1, shopName)
              }
            "
            >-</span
          >
          <span class="product__number__count">{{
            cartList?.[shopId]?.productList?.[item._id]?.count || 0
          }}</span>
          <span
            class="product__number__plus"
            @click="
              () => {
                ChangeCartItem(shopId, item._id, item, 1, shopName)
              }
            "
            >+</span
          >
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { get } from '../../utils/request'
import { reactive, toRefs, ref, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import { useCommonCartEffect } from '../../effects/commonCartEffect'
import { useStore } from 'vuex'
// const
const category = [
  { name: '全部商品', tab: 'all' },
  { name: '秒杀', tab: 'seckill' },
  { name: '新鲜水果', tab: 'fruit' }
]
// tab 切换
const useTabEffect = () => {
  const currentTab = ref(category[0].tab)
  const handleTabClick = tab => {
    currentTab.value = tab
  }
  return { currentTab, handleTabClick }
}
// 列表内容
const useCurrentListEffect = (currentTab, shopId) => {
  const content = reactive({ list: [] })
  const getContentData = async () => {
    const reslut = await get(`api/shop/${shopId}/products`, {
      tab: currentTab.value
    })
    if (reslut?.errno === 0 && reslut?.data.length) {
      content.list = reslut.data
    }
  }
  watchEffect(() => {
    getContentData()
  })
  const { list } = toRefs(content)
  return { list }
}

// 购物车
const useCartEffect = () => {
  const store = useStore()
  const { ChangeCartItemInfo, cartList } = useCommonCartEffect()
  // console.log(cartList)
  const changeShopName = (shopId, shopName) => {
    store.commit('changeShopName', { shopId, shopName })
  }
  const ChangeCartItem = (shopId, productId, productInfo, num, shopName) => {
    ChangeCartItemInfo(shopId, productId, productInfo, num)
    // changeShopName
    changeShopName(shopId, shopName)
  }
  return { ChangeCartItem, cartList }
}
export default {
  name: 'Content',
  props: ['shopName'],
  setup(props) {
    const route = useRoute()
    const shopId = route.params.id
    const { currentTab, handleTabClick } = useTabEffect()
    const { list } = useCurrentListEffect(currentTab, shopId)
    const { ChangeCartItem, cartList } = useCartEffect()
    return {
      currentTab,
      list,
      category,
      handleTabClick,
      shopId,
      ChangeCartItem,
      cartList
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../../style/common.scss';
.content {
  display: flex;
  position: absolute;
  right: 0;
  left: 0;
  top: 1.56rem;
  bottom: 0.5rem;
}
.category {
  width: 0.76rem;
  background: $search-bgColor;
  overflow-y: scroll;
  .category__item {
    line-height: 0.4rem;
    text-align: center;
    font-size: 0.14rem;
    color: $content-fontColor;
    letter-spacing: 0.01rem;
  }
  .category__item_active {
    background: $bgColor;
  }
}
.product {
  flex: 1;
  overflow-y: scroll;
  .product__item {
    position: relative;
    display: flex;
    padding: 0.12rem 0;
    margin: 0 0.16rem;
    border-bottom: 0.01rem solid $content-bgColor;
    align-items: center;
    .product__item__img {
      width: 0.68rem;
      height: 0.68rem;
      margin-right: 0.16rem;
    }
    .product__item__title {
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $content-fontColor;
      margin-bottom: 0.06rem;
      @include ellipsis;
    }
    .product__item__detail {
      overflow: hidden;
    }
    .product__item__sales {
      line-height: 0.16rem;
      font-size: 0.12rem;
      color: $content-fontColor;
      margin-bottom: 0.06rem;
    }
    .product__item__price {
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: #e93b3b;
      display: flex;
      align-items: center;
      .product__item__yen {
        font-size: 0.12rem;
      }
      .product__item__origin {
        color: #999;
        text-decoration: line-through;
        margin-left: 0.06rem;
        font-size: 0.2rem;
        transform: scale(0.5);
      }
    }
  }
  .product__number {
    position: absolute;
    right: 0;
    bottom: 0.12rem;
    display: flex;
    align-items: center;
    .product__number__count {
      margin: 0 0.1rem;
      display: inline-block;
      line-height: 0.24rem;
      text-align: center;
    }
    .product__number__minus,
    .product__number__plus {
      display: inline-block;
      width: 0.2rem;
      height: 0.2rem;
      border-radius: 50%;
      border: 0.01rem solid #666;
      text-align: center;
      line-height: 0.2rem;
      font-size: 0.2rem;
    }
    .product__number__minus {
      color: #666;
    }
    .product__number__plus {
      background: #0091ff;
      color: $bgColor;
    }
  }
}
</style>
